﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using SimioAPI;
using SimioAPI.Extensions;

namespace KIMESimioDebugStep
{
    public class DebugStepStepDefinition : IStepDefinition
    {
        #region IStepDefinition Members

        /// <summary>
        /// Property returning the full name for this type of step. The name should contain no spaces. 
        /// </summary>
        public string Name
        {
            get { return "DebugStep"; }
        }

        /// <summary>
        /// Property returning a short description of what the step does.  
        /// </summary>
        public string Description
        {
            get { return "This step visualizes the states using Message Box when it is executed."; }
        }

        /// <summary>
        /// Property returning an icon to display for the step in the UI. 
        /// </summary>
        public System.Drawing.Image Icon
        {
            get { return null; }
        }

        /// <summary>
        /// Property returning a unique static GUID for the step.  
        /// </summary>
        public Guid UniqueID
        {
            get { return MY_ID; }
        }
        static readonly Guid MY_ID = new Guid("{860D8870-7004-11DF-A686-F8DCDFD72085}");

        /// <summary>
        /// Property returning the number of exits out of the step. Can return either 1 or 2. 
        /// </summary>
        public int NumberOfExits
        {
            get { return 1; }
        }

        /// <summary>
        /// Method called that defines the property schema for the step.
        /// </summary>
        public void DefineSchema(IPropertyDefinitions schema)
        {
            // Example of how to add a property definition to the step.
            IPropertyDefinitions pd;
            pd = schema.AddRepeatGroup("DebugExpressions", "The states to debug.");

            IPropertyDefinition expressionNameProperty = pd.AddStringProperty("DebugName", String.Empty);
            expressionNameProperty.Description = "Give the expression a name.";

            IPropertyDefinition expressionProperty = pd.AddExpressionProperty("DebugExpression", String.Empty);
            expressionProperty.Description = "The expression to debug.";

        }

        /// <summary>
        /// Method called to create a new instance of this step type to place in a process. 
        /// Returns an instance of the class implementing the IStep interface.
        /// </summary>
        public IStep CreateStep(IPropertyReaders properties)
        {
            return new DebugStep(properties);
        }

        #endregion
    }

    class DebugStep : IStep
    {
        IPropertyReaders _properties;
        IRepeatingPropertyReader _expressionsToDebug;
        bool _debug;
        private double _lastSimTime;

        public DebugStep(IPropertyReaders properties)
        {
            _properties = properties;
            _expressionsToDebug = (IRepeatingPropertyReader)_properties.GetProperty("DebugExpressions");
            _debug = true;
            _lastSimTime = 0;
        }

        #region IStep Members

        /// <summary>
        /// Method called when a process token executes the step.
        /// </summary>
        public ExitType Execute(IStepExecutionContext context)
        {
            if (_lastSimTime > context.Calendar.TimeNow)
                _debug = true;

            if (_debug)
            {
                string debugString = "KIME DEBUG STEP\n\n";
                IPropertyReader currentExp;
                IPropertyReader currentExpName;

                int numberOf = _expressionsToDebug.GetCount(context);

                for (int i = 0; i < numberOf; i++)
                {
                    currentExp = _expressionsToDebug.GetRow(i, context).GetProperty("DebugExpression");
                    currentExpName = _expressionsToDebug.GetRow(i, context).GetProperty("DebugName");

                    debugString += currentExpName.GetStringValue(context) + ": " + currentExp.GetDoubleValue(context).ToString() + "\n";
                }

                debugString += "\nContinue debugging?";



                if (MessageBox.Show(debugString, "KIME DEBUG STEP", MessageBoxButtons.YesNo) == DialogResult.No)
                    _debug = false;


            }

            _lastSimTime = context.Calendar.TimeNow;
            return ExitType.FirstExit;
        }

        #endregion
    }
}
